using Rocket.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using UnityEngine;
using Dll = System.Reflection.Assembly;
using Logger = Rocket.Core.Logging.Logger;

namespace AroPl.Addons
{
    public class Loader
    {
        /// <summary>
        /// 在线加载数据库(若有缓存则从本地加载)
        /// </summary>
        /// <param name="Directory">文件夹</param>
        public static void LoadLibraries(string Directory)
        {
            using (WebClient web = new WebClient())
            {
                try
                {
                    // 支持库不建议使用Assembly方法加载，建议参考新版本Aurora Plugin Loader中的Library加载法
                    if (!File.Exists(Directory + @"\I18N.CJK.res"))
                    {
                        web.DownloadFile("https://yunstation.work/AuroraFiles/Downloads/I18N.CJK.dll", Directory + @"\Resources\I18N.CJK.res");
                    }
                    Dll.Load(File.ReadAllBytes(Directory + @"\Resources\I18N.CJK.res"));
                    if (!File.Exists(Directory + @"\Resources\I18N.res"))
                    {
                        web.DownloadFile("https://yunstation.work/AuroraFiles/Downloads/I18N.dll", Directory + @"\Resources\I18N.res");
                    }
                    Dll.Load(File.ReadAllBytes(Directory + @"\Resources\I18N.res"));
                    if (!File.Exists(Directory + @"\Resources\I18N.West.res"))
                    {
                        web.DownloadFile("https://yunstation.work/AuroraFiles/Downloads/I18N.West.dll", Directory + @"\Resources\I18N.West.res");
                    }
                    Dll.Load(File.ReadAllBytes(Directory + @"\Resources\I18N.West.res"));
                    if (!File.Exists(Directory + @"\Resources\MySql.Data.res"))
                    {
                        web.DownloadFile("https://yunstation.work/AuroraFiles/Downloads/MySql.Data.dll", Directory + @"\Resources\MySql.Data.res");
                    }
                    Dll.Load(File.ReadAllBytes(Directory + @"\Resources\MySql.Data.res"));
                    if (!File.Exists(Directory + @"\Resources\SqlSugar.res"))
                    {
                        web.DownloadFile("https://yunstation.work/AuroraFiles/Downloads/SqlSugar.dll", Directory + @"\Resources\SqlSugar.res");
                    }
                    Dll.Load(File.ReadAllBytes(Directory + @"\Resources\SqlSugar.res"));
                    if (!File.Exists(Directory + @"\Resources\System.Management.res"))
                    {
                        web.DownloadFile("https://yunstation.work/AuroraFiles/Downloads/System.Management.dll", Directory + @"\Resources\System.Management.res");
                    }
                    Dll.Load(File.ReadAllBytes(Directory + @"\Resources\System.Management.res"));
                }
                catch (Exception ex)
                {
                    Logger.Log("[极光·未来] - AroPl - 加载支持库出错", ConsoleColor.Yellow);
                    Logger.LogException(ex);
                    return;
                }
            }
        }

        public static List<GameObject> GetGameEntry(Type type)
        {
            try
            {
                return (List<GameObject>)type.GetField("plugins", BindingFlags.NonPublic | BindingFlags.Static).GetValue(R.Plugins);
            }
            catch
            {
                try
                {
                    return (List<GameObject>)type.GetField("plugins", BindingFlags.Public | BindingFlags.Static).GetValue(R.Plugins);
                }
                catch
                {
                    try
                    {
                        return (List<GameObject>)type.GetField("plugins", BindingFlags.Public).GetValue(R.Plugins);
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
    }
}
